<template>
  <div v-show="tableData.length">
    <header class="game_modal_header">运动员排行榜</header>
    <div class="schedule_content">
      <div class="gl_clear">
        <ul class="athlete_list gl_fl">
          <li
            v-for="(item, index) in tableData"
            :key="index"
            :class="{active: index === activeIndex}"
            class="athlete_item gl_clear"
            @mouseenter="changeAthleteIndex(index)"
          >
            <div
              class="medal_wrap rank gl_fl"
              :class="{rank_1: item.finalRank === 1, rank_2: item.finalRank === 2, rank_3: item.finalRank === 3}"
            >
              {{ item.finalRank }}
            </div>
            <div
              class="athlete_score gl_clear"
              :class="{gold_bg: item.finalRank === 1, silver_bg: item.finalRank === 2, bronze_bg: item.finalRank === 3}"
            >
              <span class="athlete_name gl_fl">{{ item.name }}</span>
              <span class="athlete_sc gl_fr">积分：{{ item.finalPoints }}</span>
            </div>
          </li>
        </ul>
        <div v-if="activeAthlete" class="athlete_info gl_fl">
          <div class="athlete_face_wrap gl_fl">
            <img class="athlete_face" :src="getStaticFileUrl(activeAthlete.photoUrl)" @error="errorPhoto">
          </div>
          <div class="athlete_data">
            <div class="athlete_medal_wrap">
              <div
                class="medal_wrap rank gl_fl"
                :class="{rank_1: activeAthlete.finalRank === 1, rank_2: activeAthlete.finalRank === 2, rank_3: activeAthlete.finalRank === 3}"
              >
                {{ activeAthlete.finalRank }}
              </div>
            </div>
            <div class="athlete_data_name">{{ activeAthlete.name }}</div>
            <div class="athlete_data_org">{{ activeAthlete.organization }}</div>
            <div class="athlete_win">
              <span class="color_red">{{ activeAthlete.winCount }}胜</span>
              <span class="color_red"> / </span>
              <span class="color_gray">{{ activeAthlete.loseCount }}负</span>
            </div>
            <div class="athlete_win_rate color_red">胜率：{{ (activeAthlete.winPercent * 100).toString().match(/^\d+(?:\.\d{0,2})?/)[0] }}%</div>
            <ul class="athlete_medal_num">
              <li>
                <div class="medals_wrap">
                  <img class="medals_img" src="@/assets/images/league/gold_big.png" alt="">
                </div>
                <div class="medals_num">{{ activeAthlete.subRankList && activeAthlete.subRankList[0] ? activeAthlete.subRankList[0].subRankCount : '' }}</div>
              </li>
              <li>
                <div class="medals_wrap">
                  <img class="medals_img" src="@/assets/images/league/silver_big.png" alt="">
                </div>
                <div class="medals_num">{{ activeAthlete.subRankList && activeAthlete.subRankList[1] ? activeAthlete.subRankList[1].subRankCount : '' }}</div>
              </li>
              <li>
                <div class="medals_wrap">
                  <img class="medals_img" src="@/assets/images/league/bronze_big.png" alt="">
                </div>
                <div class="medals_num">{{ activeAthlete.subRankList && activeAthlete.subRankList[2] ? activeAthlete.subRankList[2].subRankCount : '' }}</div>
              </li>
            </ul>
          </div>
          <div class="athlete_score_flag">积分：{{ activeAthlete.finalPoints }}</div>
        </div>
      </div>
      <div class="more_wrap gl_clear">
        <span class="more_btn pointer gl_fr" @click="jumpToIntegral">更多</span>
      </div>
    </div>
  </div>
</template>

<script>
import {
  api_GET_EventsManagementEventResultUnifiedPointRankList
} from '@/extend/api/gameswaggerApi/app';

import {
  Api_GET_EventsManagementEventResultUnifiedPointRankListQO
} from '@/lib/form/game/swaggerForm';

import defaultPhoto from '@/assets/images/face_default.png';

export default {
  props: {
    gameInfo: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    return {
      activeIndex: 0,
      defaultPhoto: defaultPhoto, // 默认照片
      tableData: [],
      qo: new Api_GET_EventsManagementEventResultUnifiedPointRankListQO()
    };
  },
  computed: {
    activeAthlete () {
      return this.tableData[this.activeIndex];
    }
  },
  watch: {
    gameInfo: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.id) {
          this.getPageList();
        }
      }
    }
  },
  methods: {
    // 获取页面数据
    getPageList () {
      const _qo = JSON.parse(JSON.stringify(this.qo));
      _qo.CompetitionId = this.gameInfo.id;
      _qo.Type = 'Register';
      _qo.Page = 1;
      _qo.SkipCount = 0;
      delete _qo.EventTypeCode;
      _qo.IncludeFinal = true;
      _qo.IncludeSubRank = true;
      this.apiRankList(_qo);
    },
    // UnifiedPointRankList
    apiRankList(qo) {
      api_GET_EventsManagementEventResultUnifiedPointRankList(qo)
        .then(
          res => {
            this.tableData = res.items;
            console.log(res);
          }
        )
        .catch(
          error => {
            console.log(error);
          }
        );
    },
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPhoto;
      img.onerror = null;
    },
    // 跳转至积分排行
    jumpToIntegral () {
      this.$router.push({
        name: 'Integral'
      });
    },
    changeAthleteIndex (index) {
      this.activeIndex = index;
    }
  }
};
</script>

<style lang="scss" scoped>
  .pointer {
    cursor: pointer;
  }
  .color_red {
    color: #de1c1c;
  }
  .color_gray {
    color: #989898;
  }

  .athlete_list {
    width: 520px;
    min-height: 436px;
    margin-right: 20px;
  }
  .athlete_item {
    height: 40px;
    margin-bottom: 4px;
  }
  .medal_wrap {
    width: 38px;
    height: 100%;
    display: flex;
    justify-content: center;
    color: #fff;
    align-items: center;
  }
  .athlete_score {
    position: relative;
    margin-left: 50px;
    padding: 0 23px;
    height: 100%;
    line-height: 40px;
    border-radius: 8px;
    background-color: #fff;
    box-shadow: 0px 10px 16px 0px rgba(0,0,0,0.10);
  }
  .athlete_name {
    font-size: 16px;
    font-weight: bold;
    color: #353434;
  }
  .athlete_sc {
    font-size: 16px;
    font-weight: bold;
    color: #de1c1c;
  }
  .active .athlete_score::before {
    display: block;
    position: absolute;
    right: -8px;
    top: 12px;
    content: "";
    border: 8px solid #DC1D1D;
    border-left-color: transparent;
    border-top-color: transparent;
    border-radius: 4px;
    transform: rotate(-45deg);
  }

  .athlete_info {
    position: relative;
    width: 580px;
    height: 436px;
    box-sizing: border-box;
    padding: 40px 0;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0px 10px 30px 0px rgba(0,0,0,0.10);
  }
  .athlete_face_wrap {
    box-sizing: border-box;
    width: 288px;
    height: 354px;
    overflow: hidden;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .athlete_face {
    width: 100%;
    border-radius: 8px;
  }
  .athlete_data {
    margin-left: 288px;
    padding: 0 20px;
  }
  .athlete_score_flag {
    position: absolute;
    min-width: 108px;
    top: 0;
    right: 10px;
    padding: 10px 12px;
    font-size: 14px;
    text-align: center;
    white-space: nowrap;
    color: #fff;
    line-height: 24px;
    // background-color: #de1c1c;
    background-image: url('../../../../../assets/images/league/single.png');
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
  .medal_img {
    width: 100%;
    height: 100%;
  }
  .athlete_medal_wrap {
    width: 42px;
    height: 42px;
    display: flex;
    justify-content: left;
    align-items: center;
    margin-bottom: 17px;
  }
  .athlete_data_name {
    font-size: 20px;
    font-weight: 500;
    color: #353434;
    line-height: 22px;
    margin-bottom: 10px;
  }
  .athlete_data_org {
    height: 64px;
    font-size: 14px;
    font-weight: 400;
    color: #353434;
    line-height: 22px;
  }
  .athlete_win {
    line-height: 25px;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 3px;
  }
  .athlete_win_rate {
    line-height: 25px;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 40px;
  }
  .athlete_medal_num {
    display: flex;
    padding-right: 40px;
    justify-content: space-between;
  }
  .medals_img {
    width: 33px;
    height: 44px;
    margin-bottom: 6px;
  }
  .medals_num {
    font-size: 14px;
    text-align: center;
    color: #353434;
    line-height: 22px;
  }
  .medals_wrap {
    width: 33px;
    height: 44px;
    margin-bottom: 5px;
  }

  .gold_bg {
    background: linear-gradient(135deg,#ffd159, #fff3b5);
  }
  .silver_bg {
    background: linear-gradient(135deg,#a4c7ec, #e4f9ff);
  }
  .bronze_bg {
    background: linear-gradient(135deg,#dec6af, #fef0e1);
  }
  .rank {
    background-repeat: no-repeat;
    background-image: url('../../../../../assets/images/league/rank.png');
    background-size: 38px 40px;
  }
  .rank_1 {
    background-image: url('../../../../../assets/images/league/rank_1.png');
  }
  .rank_2 {
    background-image: url('../../../../../assets/images/league/rank_2.png');
  }
  .rank_3 {
    background-image: url('../../../../../assets/images/league/rank_3.png');
  }

  .more_wrap {
    padding: 20px;
  }
  .more_btn {
    color: #D01528;
    font-size: 12px;
  }
</style>
